<template>
  <div class="flex-wrap">
    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="90px"
      >
        <el-form-item label="系统模块：" prop="keyword">
          <el-input
            v-model="queryParams.keyword"
            placeholder="请输入股票名称或股票代码"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="操作时间" prop="registerTime">
          <el-date-picker
            v-model="queryParams.registerTime"
            type="daterange"
            value-format="YYYY-MM-DD"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item label="操作类型：" prop="memberType">
          <el-select
            v-model="queryParams.memberType"
            placeholder="请选择"
            clearable
            style="width: 180px"
          >
            <el-option
              v-for="item in memberOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <el-icon><Search /></el-icon>查询
          </el-button>
          <el-button @click="resetQuery">
            <el-icon><Refresh /></el-icon>重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 表格区域 -->
    <el-card class="table-card">
      <template #header>
        <el-button type="primary" @click="exportData">批量导出</el-button>
      </template>
      <el-table
        v-loading="loading"
        :data="userList"
        @selection-change="handleSelectionChange"
        border
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" type="index" width="60" align="center" />
        <el-table-column label="系统模块" prop="nickname" />
        <el-table-column label="操作类型" prop="nickname" align="center" />
        <el-table-column label="操作人员" prop="nickname" align="center" />
        <el-table-column label="部门名称" prop="tokenAmount" align="center" />
        <el-table-column label="操作地址" prop="lastLoginTime" align="center" />
        <el-table-column label="操作时间" prop="registerTime" align="center" />
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="queryParams.pageNum"
          v-model:page-size="queryParams.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { Search, Refresh } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';

// 会员类型选项
const memberOptions = [
  { value: 'normal', label: '普通用户' },
  { value: 'vip', label: 'VIP会员' },
  { value: 'svip', label: 'SVIP会员' },
];

// 状态选项
const statusOptions = [
  { value: 'online', label: '在线' },
  { value: 'offline', label: '离线' },
  { value: 'banned', label: '封禁' },
];

// 查询参数
const initValues = () => {
  return {
    pageNum: 1,
    pageSize: 10,
    keyword: '',
    registerTime: [],
    memberType: '',
  };
};
let queryParams = reactive(initValues());

const loading = ref(false);
const userList = ref([]);
const total = ref(0);
const selectedUsers = ref([]);

// 标签类型映射函数
const getMemberTagType = (type) => {
  const map = {
    normal: 'info',
    vip: 'warning',
    svip: 'danger',
  };
  return map[type] || 'info';
};

const getStatusTagType = (status) => {
  const map = {
    online: 'success',
    offline: 'info',
    banned: 'danger',
  };
  return map[status] || 'info';
};

// 获取标签文本
const getMemberLabel = (type) => {
  const member = memberOptions.find((item) => item.value === type);
  return member ? member.label : '普通用户';
};

const getStatusLabel = (status) => {
  const statusItem = statusOptions.find((item) => item.value === status);
  return statusItem ? statusItem.label : '未知';
};
//
const exportData = () => {};
// 查询用户列表
const getList = async () => {
  // loading.value = true;
  // 处理日期范围
  const params = { ...queryParams };
  if (params.registerTime && params.registerTime.length === 2) {
    params.registerTimeStart = params.registerTime[0];
    params.registerTimeEnd = params.registerTime[1];
  }
  delete params.registerTime;
  // getUserList(params).then(({data}) =>{
  //   console.log(data)
  //   total.value = data.total;
  //   userList.value = data.list
  // }).finally(() => {
  //   loading.value = false;
  // });
};

// 处理查询
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

// 重置查询
const resetQuery = () => {
  Object.assign(queryParams, initValues());
  handleQuery();
};

// 处理分页大小变更
const handleSizeChange = (size) => {
  queryParams.pageSize = size;
  getList();
};

// 处理页码变更
const handleCurrentChange = (page) => {
  queryParams.pageNum = page;
  getList();
};

// 处理表格选择变更
const handleSelectionChange = (selection) => {
  selectedUsers.value = selection;
};

// 初始加载数据
onMounted(() => {
  getList();
});
</script>

<style lang="less" scoped>
.search-card,
.table-card {
  margin-bottom: 20px;

  .el-card__header {
    padding: 15px 20px;
  }
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .header-buttons {
    display: flex;
    gap: 10px;
  }
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.text-muted {
  font-size: 13px;
  color: #909399;
  margin-top: 5px;
}
</style>
